package com.zqwh.yj.sdk.model;

import com.zqwh.yj.sdk.utils.ByteUtils;
import com.zqwh.yj.sdk.utils.SerialPortUtils;
import com.zqwh.yj.sdk.utils.ThreadPoolManager;

import java.util.LinkedList;
import java.util.Queue;

public class RelayModelImpl implements RelayModel {
    private SerialPortUtils spuLockCtl;
    private RelayReadDoorStatusListener readDoorStatusListener;
    private Queue<byte[]> cmdQueue;

    public RelayModelImpl(String uartPath, int baudrate) {
        initLight(uartPath, baudrate);
        cmdQueue = new LinkedList<>();
        ThreadPoolManager.execute(new Runnable() {
            @Override
            public void run() {
                synchronized (this) {
                    while (true) {
                        try {
                            byte[] cmd = cmdQueue.peek();
                            if (cmd != null) {
                                spuLockCtl.sendSerialPort(cmd);
                                cmdQueue.remove();
                            }
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }

    private void initLight(String uartPath, int baudrate) {
        spuLockCtl = new SerialPortUtils();
        spuLockCtl.openSerialPort("light", uartPath, baudrate);
        spuLockCtl.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() {
            @Override
            public void onDataReceive(byte[] buffer, int size) {
                if (readDoorStatusListener != null) {
                    String ret = ByteUtils.byteArrayToHexString(buffer);
                    if (ret.startsWith("483A01410000000000000010")) {
                        readDoorStatusListener.onReadDoorStatusFinishedListener(RelayReadDoorStatusListener.DOOR_STATUS_CLOSE);
                    } else if (ret.startsWith("483A01410000000000000000")) {
                        readDoorStatusListener.onReadDoorStatusFinishedListener(RelayReadDoorStatusListener.DOOR_STATUS_OPEN);
                    }
                }
            }
        });
    }

    @Override
    public void lightOn(int number) {
        byte[] cmd = null;
        switch (number){
            case 1:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 21 22 22 22 22 22 22 22 E9 45 44".replace(" ",""));
                break;
            case 2:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 12 22 22 22 22 22 22 22 DA 45 44".replace(" ",""));
                break;
            case 3:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 21 22 22 22 22 22 22 E9 45 44".replace(" ",""));
                break;
            case 4:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 12 22 22 22 22 22 22 DA 45 44".replace(" ",""));
                break;
            case 5:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 21 22 22 22 22 22 E9 45 44".replace(" ",""));
                break;
            case 6:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 12 22 22 22 22 22 DA 45 44".replace(" ",""));
                break;
            case 7:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 21 22 22 22 22 E9 45 44".replace(" ",""));
                break;
            case 8:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 12 22 22 22 22 DA 45 44".replace(" ",""));
                break;
            case 9:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 21 22 22 22 E9 45 44".replace(" ",""));
                break;
            case 10:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 12 22 22 22 DA 45 44".replace(" ",""));
                break;
            case 11:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 21 22 22 E9 45 44".replace(" ",""));
                break;
            case 12:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 12 22 22 DA 45 44".replace(" ",""));
                break;
            case 13:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 21 22 E9 45 44".replace(" ",""));
                break;
            case 14:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 12 22 DA 45 44".replace(" ",""));
                break;
            case 15:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 22 21 E9 45 44".replace(" ",""));
                break;
        }
        cmdQueue.offer(cmd);
    }

    @Override
    public void lightOff(int number) {
        byte[] cmd = null;
        switch (number){
            case 1:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 20 22 22 22 22 22 22 22 E8 45 44".replace(" ",""));
                break;
            case 2:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 02 22 22 22 22 22 22 22 CA 45 44".replace(" ",""));
                break;
            case 3:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 20 22 22 22 22 22 22 E8 45 44".replace(" ",""));
                break;
            case 4:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 02 22 22 22 22 22 22 CA 45 44".replace(" ",""));
                break;
            case 5:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 20 22 22 22 22 22 E8 45 44".replace(" ",""));
                break;
            case 6:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 02 22 22 22 22 22 CA 45 44".replace(" ",""));
                break;
            case 7:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 20 22 22 22 22 E8 45 44".replace(" ",""));
                break;
            case 8:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 02 22 22 22 22 CA 45 44".replace(" ",""));
                break;
            case 9:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 20 22 22 22 E8 45 44".replace(" ",""));
                break;
            case 10:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 02 22 22 22 CA 45 44".replace(" ",""));
                break;
            case 11:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 20 22 22 E8 45 44".replace(" ",""));
                break;
            case 12:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 02 22 22 CA 45 44".replace(" ",""));
                break;
            case 13:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 20 22 E8 45 44".replace(" ",""));
                break;
            case 14:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 02 22 CA 45 44".replace(" ",""));
                break;
            case 15:
                cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 22 20 E8 45 44".replace(" ",""));
                break;
        }
        cmdQueue.offer(cmd);
    }

    public void lightOnAll() {
        byte[] cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 11 11 11 11 11 11 11 21 72 45 44".replace(" ",""));
        spuLockCtl.sendSerialPort(cmd);
    }

    public void lightOffAll() {
        byte[] cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 00 00 00 00 00 00 00 20 FA 45 44".replace(" ",""));
        cmdQueue.offer(cmd);
    }

    @Override
    public void unlock() {
        byte[] cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 22 12 DA 45 44".replace(" ",""));
        cmdQueue.offer(cmd);
    }

    @Override
    public void lock() {
        byte[] cmd = ByteUtils.hexStringToByteArray("48 3a 01 57 22 22 22 22 22 22 22 02 CA 45 44".replace(" ",""));
        cmdQueue.offer(cmd);
    }

    @Override
    public void readDoorStatus(RelayReadDoorStatusListener listener) {
        readDoorStatusListener = listener;
        byte[] cmd = ByteUtils.hexStringToByteArray("48 3a 01 52 00 00 00 00 00 00 00 00 d5 45 44".replace(" ",""));
        cmdQueue.offer(cmd);
    }
}
